package com.wawayaya.db;

import android.content.Context;
import android.util.Log;
import net.sqlcipher.database.SQLiteDatabase;
import net.sqlcipher.database.SQLiteDatabase.CursorFactory;
import net.sqlcipher.database.SQLiteOpenHelper;
import de.greenrobot.dao.AbstractDaoMaster;
import de.greenrobot.dao.identityscope.IdentityScopeType;

import com.wawayaya.bean.Area;
import com.wawayaya.db.basedao.AdvertDao;
import com.wawayaya.db.basedao.AreaDao;
import com.wawayaya.db.basedao.BannerDao;
import com.wawayaya.db.basedao.BookShelfDao;
import com.wawayaya.db.basedao.BooksDao;
import com.wawayaya.db.basedao.ConfigDao;
import com.wawayaya.db.basedao.HomePageDao;
import com.wawayaya.db.basedao.NoticeDao;
import com.wawayaya.db.basedao.SupportDao;
import com.wawayaya.db.basedao.UserDao;

/** 
 * Master of DAO (schema version 3): knows all DAOs.
*/
public class DaoMaster extends AbstractDaoMaster {
    public static final int SCHEMA_VERSION = 1;

    /** Creates underlying database table using DAOs. */
    public static void createAllTables(SQLiteDatabase db, boolean ifNotExists) {

        BooksDao.createTable(db, ifNotExists);
        UserDao.createTable(db,ifNotExists);
        AreaDao.createTable(db,ifNotExists);
        BannerDao.createTable(db, ifNotExists);
        ConfigDao.createTable(db, ifNotExists);
        SupportDao.createTable(db, ifNotExists);
        NoticeDao.createTable(db,ifNotExists);
        BookShelfDao.createTable(db, ifNotExists);
        HomePageDao.createTable(db,ifNotExists);
        AdvertDao.createTable(db, ifNotExists);
    }
    
    /** Drops underlying database table using DAOs. */
    public static void dropAllTables(SQLiteDatabase db, boolean ifExists) {

        BooksDao.dropTable(db, ifExists);
        UserDao.dropTable(db,ifExists);
        AreaDao.dropTable(db,ifExists);
        BannerDao.dropTable(db, ifExists);
        ConfigDao.dropTable(db, ifExists);
        SupportDao.dropTable(db,ifExists);
        NoticeDao.dropTable(db,ifExists);
        BookShelfDao.dropTable(db,ifExists);
        HomePageDao.dropTable(db, ifExists);
        AdvertDao.dropTable(db, ifExists);
    }
    
    public static abstract class OpenHelper extends SQLiteOpenHelper {
        public OpenHelper(Context context, String name, CursorFactory factory) {
            super(context, name, factory, SCHEMA_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            Log.i("greenDAO", "Creating tables for schema version " + SCHEMA_VERSION);
            createAllTables(db, false);
        }
    }
    
    /** WARNING: Drops all table on Upgrade! Use only during development. */
    public static class DevOpenHelper extends OpenHelper {
        public DevOpenHelper(Context context, String name, CursorFactory factory) {
            super(context, name, factory);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.i("greenDAO", "Upgrading schema from version " + oldVersion + " to " + newVersion + " by dropping all tables");
            dropAllTables(db, true);
            onCreate(db);
        }
    }

    public DaoMaster(SQLiteDatabase db) {
        super(db, SCHEMA_VERSION);

        registerDaoClass(BooksDao.class);
        registerDaoClass(UserDao.class);
        registerDaoClass(AreaDao.class);
        registerDaoClass(BannerDao.class);
        registerDaoClass(ConfigDao.class);
        registerDaoClass(SupportDao.class);
        registerDaoClass(NoticeDao.class);
        registerDaoClass(BookShelfDao.class);
        registerDaoClass(HomePageDao.class);
        registerDaoClass(AdvertDao.class);
    }
    
    public DaoSession newSession() {
        return new DaoSession(db, IdentityScopeType.Session, daoConfigMap);
    }
    
    public DaoSession newSession(IdentityScopeType type) {
        return new DaoSession(db, type, daoConfigMap);
    }
    
}
